package com.wanglibao.woodtrade.woodtrade.ui.activity.user;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.model.User;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.activity.BaseActivity;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @Bind(R.id.rl_common_title)
    RelativeLayout rlCommonTitle;
    @Bind(R.id.edit_activity_login_phone)
    EditText editActivityLoginPhone;
    @Bind(R.id.edit_activity_login_password)
    EditText editActivityLoginPassword;
    @Bind(R.id.btn_activity_login_login)
    Button btn_activity_login_login;
    @Bind(R.id.tv_activity_login_forget)
    TextView tv_activity_login_forget;
    @Bind(R.id.tv_activity_login_register)
    TextView tv_activity_login_register;
    @Bind(R.id.sv_activity_login_view)
    ScrollView sv_activity_login_view;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 != 2){
                ToastUtil.createLongToast(LoginActivity.this,"登录成功");
                UserImpl userImpl = new UserImpl(LoginActivity.this);
                userImpl.deleteAll();
                User user = new Gson().fromJson(msg.obj.toString(), User.class);
                user.password = editActivityLoginPassword.getText().toString();
                userImpl.insert(user);
                finish();
            }

            return false;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            rlCommonTitle.setPadding(0, 0, 0, 0);
        }
        tvTitle.setText("登录");
        initView();
    }

    private void initView() {
        addViewClicklistener(btn_activity_login_login,tvRight,tvBack);

        if (Build.VERSION.SDK_INT >= 19) {
            final View decorView = getWindow().getDecorView();
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect rect = new Rect();
                    decorView.getWindowVisibleDisplayFrame(rect);
                    int screenHeight = decorView.getRootView().getHeight();
                    int height = screenHeight - rect.bottom;
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) sv_activity_login_view.getLayoutParams();
                    layoutParams.setMargins(0, 0, 0, height);
                    sv_activity_login_view.requestLayout();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.btn_activity_login_login:
                String phone = editActivityLoginPhone.getText().toString();
                String password = editActivityLoginPassword.getText().toString();
                if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)){
                    JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this, NetConfig.SERVER+NetConfig.TYPE_USER,
                            NetConfig.userLogin,handler,"jsonObject",NetConfig.TaskId.userLogin,new Object[]{phone,password});
                    jsonRPCAsyncTask.execute(new Void[]{});
                }else{
                    ToastUtil.createLongToast(this,"用户名和密码不能为空");
                }
                break;
        }
    }

    @OnClick(R.id.tv_activity_login_forget)
    void forgetClick(){
        startActivity(new Intent(this,ForgetPasswordActivity.class));
    }

    @OnClick(R.id.tv_activity_login_register)
    void registerClick(){
        startActivity(new Intent(this,RegisterActivity.class));
    }

}
